<template>
	<view>
		<view v-for="(section, sIdx) in localSections" :key="sIdx" class="section-block">
			<view class="section-header" @click="toggleSection(sIdx)">
				<text>{{ sIdx + 1 }}. {{ section.name }}</text>
				<view class="picker-btn-down">
					<image :src="HTTP_IMG_URL()+'/static/images/arrow-down.png'" model="aspectFill" :class="section.open?'up':'down'"></image>
				</view>
			</view>
			<view v-show="section.open" class="section-lessons">
				<view v-for="(item, index) in section.list" :key="index" class="lesson-row">
					<view class="lesson-info-buy">
						<text class="lesson-title">{{ item.name }}</text>
						<view class="u-flex u-flex-start u-flex-items-center">
							<view v-if="item.is_charge === 2" class="trial-tag">可试看</view>
						</view>
					</view>
					<view class="btn-study-row u-flex u-flex-items-center">
						<view class="btn-study" @click="onStartLesson(item)">
							{{ item.is_charge === 2 || formType === 'yigou' ? '观看' : '购买课程' }}
						</view>
					</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
import { HTTP_IMG_URL } from "@/config/app";
export default {
	name: 'CourseCatalog',
	props: {
		sections: {
			type: Array,
			required: true,
		},
		formType: {
			type: String
		}
	},
	data() {
		return {
		  localSections: JSON.parse(JSON.stringify(this.sections))
		}
	},
	methods: {
		HTTP_IMG_URL() {
			return this.$imgUrl;
		},
		toggleSection(idx) {
			this.localSections[idx].open = !this.localSections[idx].open;
			this.$forceUpdate();
		},
		onStartLesson(lesson) {
			this.$emit('startLesson', lesson);
		},
		jump(url){
			uni.navigateTo(
				{url:url}
			)
		}
	},
	watch: {
		sections: {
			handler(newVal) {
				this.localSections = JSON.parse(JSON.stringify(newVal));
			},
			deep: true
		}
	}
}
</script>

<style scoped lang="scss">
.section-block {
	margin-bottom: 16rpx;
	border-bottom: 1rpx solid #f0f0f0;
}
.section-header {
	display: flex;
	justify-content: space-between;
	align-items: center;
	font-size: 28rpx;
	padding: 16rpx 0;
	cursor: pointer;
	background: none;
}
.section-header text {
	color: #14161E;
}
.section-header uni-icons {
	margin-left: 12rpx;
}
.section-lessons {
	padding-left: 16rpx;
}
.lesson-row {
	display: flex;
	align-items: center;
	justify-content: space-between;
	background: #F5F4F9;
	border-radius: 12rpx;
	margin: 12rpx 0;
	padding: 24rpx 20rpx;
}
.lesson-info {
	display: flex;
	flex-direction: column;
}
.lesson-info-buy{
	display: flex;
	align-items: center;
}
.lesson-title {
	font-size: 26rpx;
	color: #333;
}
.lesson-time {
	color: #888;
	font-size: 22rpx;
	margin-top: 4rpx;
}
.lesson-progress {
	color: #bbb;
	font-size: 20rpx;
}
.btn-study {
	background: #2563eb;
	color: #fff;
	border-radius: 20rpx;
	font-size: 24rpx;
	padding: 8rpx 24rpx;
}
.btn-study[disabled] {
	background: #ccc;
	color: #fff;
}
.trial-tag {
	margin-left: 16rpx;
	padding: 0 16rpx;
	height: 40rpx;
	display: flex;
	justify-content: center;
	align-items: center;
	border: 2rpx solid #2563eb;
	color: #2563eb;
	background: rgba(22,100,255,0.11);
	border-top-right-radius: 22rpx;
	border-top-left-radius: 22rpx;
	border-bottom-right-radius: 22rpx;
	font-size: 20rpx;
}
.picker-btn-down{
	width: 20rpx;
	height: 14rpx;
	margin-left: 18rpx;
	image{
		width: 100%;
		height: 100%;
		margin-bottom: 20rpx ;
	}
}
.up{
	transform: rotate(180deg);
}
.down{
	transform: rotate(0deg);
}
</style> 